package rsj.dao;

import com.rlyy.aibase.utils.base.TableNode;
import rsj.table.SYS_ORGPTYPE;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;

import static rsj.table.SYS_ORGPTYPE.Orgp_Status_ServiceOver;
import static rsj.table.SYS_ORGPTYPE.Orgp_Type_Collect;

public class OrgPDao2 extends BaseDao {
    private static final OrgPDao2 instance = new OrgPDao2();
    private static final String tableName = "sys.sys_orgp";

    public static OrgPDao2 getInstance() {
        return instance;
    }

    public TableNode getListByOrgId(String orgId, String billmethod) {
        String fields = "orgp_id,orgp_prod_name,orgp_prod_id";
        String sql = String.format(select, fields, tableName, " orgp_org_id = ? and orgp_billmethod = ? and orgp_type = ? and orgp_orgpstatus = '1' ")
                + " order by sys_orgp.orgp_utime desc ";
        return (TableNode) invokeSql(conn -> {
            return getQueryRunner().queryLinkTableNode(conn, sql, orgId, billmethod, Orgp_Type_Collect);
        });
    }

    public TableNode getBillInfoByOrgId(String orgId, Connection conn) {
        String fields = "orgp_id,orgp_prod_id,orgp_prod_name,orgp_buyamount,orgp_useamount,orgp_utime,orgp_billmethod";
        String conditions = " orgp_org_id = ? and (orgp_status = '%s' or " + "orgp_status = '%s' or "+
                "( orgp_status = '%s' and to_char( orgp_utime, 'YYYY-MM' ) = ? ))" +
                "and orgp_orgpstatus = '1'";
        String sql = String.format(select, fields, tableName, String.format(conditions, SYS_ORGPTYPE.Orgp_Status_Service,SYS_ORGPTYPE.Orgp_Status_Ineffective, Orgp_Status_ServiceOver));
        return getQueryRunner().queryLinkTableNode(conn, sql, orgId, new SimpleDateFormat("yyyy-MM").format(new Date()));
    }
}
